package ga.wavaplayer.ui {
	import flash.events.Event;
	import ga.wavaplayer.video.WVideo;
	import flash.display.Sprite;
	import ga.wavaplayer.events.WStreamEvent;
	
	public class WPlayerUI extends Sprite{
		
		private var _config:Object;
		private var _video:WVideo;
		
		private var _controls:WControls;
		
		public function WPlayerUI(video:WVideo, config:Object = null) {
			
			_video = video;
			
			_config = {
				"colors": {
					"dark": 0x000000,
					"semidark": 0x424242,
					"medium": 0x4d4d4d,
					"semilight": 0xa9a9a9,
					"light": 0xffffff,
					"highlight": {
						"dark": 0x470a00,
						"semidark": 0x521d14,
						"medium": 0x6d0f00,
						"semilight": 0xb6402e,
						"light": 0xb66052
					}
				},
				"controls": {
					"show": true,
					"order": ["play-pause", "stop", "separator", "scrubber", "time", "souns", "volume", "separator", "fullscreen"],
					"height": 20,
					"margin": 30,
					"border": 1,
					"button_padding": 5
				}
			}
			
			if (config.width != undefined) _config.width = config.width;
			if (config.height != undefined) _config.height = config.height;
			if (config.volume != undefined) _config.volume = config.volume;
			
			if (_config.controls.show) {
				_controls = new WControls(_config);
				_controls.alpha = 1;//0.9;
				this.addChild(_controls);
			}
			
			_video.stream.addEventListener(WStreamEvent.META_DATA, _metadataHandler);
			_video.addEventListener(Event.ENTER_FRAME, _videoEnterFrameHandler);
		}
		
		public function resize(pWidth:int, pHeight:int ):void {
			_controls.resize(pWidth - (_config.controls.margin * 2), _config.controls.height)
			_controls.x = _config.controls.margin;
			_controls.y = pHeight - _config.controls.margin - _config.controls.height;
		}
		
		private function _metadataHandler(e:WStreamEvent):void {
			_controls.timeBox.setTotalTime(_video.stream.meta_data.duration);
			_controls.seekBar.timeTotal = _video.stream.meta_data.duration;
		}
		
		private function _videoEnterFrameHandler(e:Event):void {
			_controls.timeBox.setCurrentTime(_video.stream.time);
			_controls.seekBar.timeCurrent = _video.stream.time;
			if(_video.stream.bytesLoaded > 0 && _video.stream.bytesTotal > 0)
				_controls.seekBar.timeLoadedRatio = _video.stream.bytesLoaded / _video.stream.bytesTotal;
		}
	}
}
